package com.itcast.donkey.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.application.MyApplication;
import com.itcast.donkey.modles.DbAccountModles;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lanouhn on 16/9/9.
 */
public class ConsumeNotesActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout linScenicSpot;
    private ImageView backScenicApot;
    private ImageView ivChanScenicSpot;
    private TextView tvTitleScenicSpot;
    private ImageView ivToolTravel;
    private ImageView ivEither;
    private ImageView ivTraffic;
    private ImageView ivFood;
    private ImageView ivHotle;
    private ImageView ivTicket;
    private ImageView ivShopping;
    private ImageView ivEntertainment;
    private TextView tvMoney;

    private EditText et_money;
    private EditText et_detail;

    private SharedPreferences sp;

    private String currency_display;

    private int id;

    private String type = "其他";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consume_notes);

        sp = getSharedPreferences("config", Context.MODE_PRIVATE);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        linScenicSpot = (LinearLayout) findViewById(R.id.lin_scenic_spot);
        backScenicApot = (ImageView) findViewById(R.id.back_scenic_apot);
        ivChanScenicSpot = (ImageView) findViewById(R.id.iv_chan_scenic_spot);
        tvTitleScenicSpot = (TextView) findViewById(R.id.tv_title_scenic_spot);
        ivToolTravel = (ImageView) findViewById(R.id.iv_tool_travel);
        ivEither = (ImageView) findViewById(R.id.iv_either);
        ivTraffic = (ImageView) findViewById(R.id.iv_traffic);
        ivFood = (ImageView) findViewById(R.id.iv_food);
        ivHotle = (ImageView) findViewById(R.id.iv_hotle);
        ivTicket = (ImageView) findViewById(R.id.iv_ticket);
        ivShopping = (ImageView) findViewById(R.id.iv_shopping);
        ivEntertainment = (ImageView) findViewById(R.id.iv_entertainment);
        tvMoney = (TextView) findViewById(R.id.tv_money);

        et_money = (EditText) findViewById(R.id.et_money);
        et_detail = (EditText) findViewById(R.id.et_detail);
    }

    private void initData() {
        Intent intent = getIntent();
        currency_display = intent.getStringExtra("currency_display");
        tvMoney.setText(currency_display);
        id = sp.getInt("id", 0);
    }

    private void initEvent() {
        backScenicApot.setOnClickListener(this);
        ivChanScenicSpot.setOnClickListener(this);
        ivToolTravel.setOnClickListener(this);
        ivEither.setOnClickListener(this);
        ivTraffic.setOnClickListener(this);
        ivFood.setOnClickListener(this);
        ivTicket.setOnClickListener(this);
        ivShopping.setOnClickListener(this);
        ivEntertainment.setOnClickListener(this);
        ivHotle.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scenic_apot:
            case R.id.iv_chan_scenic_spot:
                finish();
                break;
            case R.id.iv_tool_travel:
                long time = System.currentTimeMillis();
                SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
                Date curDate = new Date(time);
                String string = format.format(curDate);
                MyApplication application = new MyApplication();
                DbManager db = x.getDb(application.getConfig());

                DbAccountModles modles = new DbAccountModles();
                modles.setBreadId(id);
                modles.setPrice(et_money.getText().toString() + currency_display);
                modles.setContent(et_detail.getText().toString() + "," + string + "记录");
                modles.setBrandName(type);
                try {
                    db.save(modles);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(ConsumeNotesActivity.this, ToolTravelActivity.class);
                intent.putExtra("currency_display", currency_display);
                startActivity(intent);
                finish();
                break;

            case R.id.iv_either:
                type = "其他";
                ivEither.setBackgroundResource(R.mipmap.account_more_pressed);
                break;
            case R.id.iv_traffic:
                type = "交通";
                ivTraffic.setBackgroundResource(R.mipmap.account_traffic_pressed);
                break;
            case R.id.iv_food:
                type = "餐饮";
                ivFood.setBackgroundResource(R.mipmap.account_food_pressed);
                break;
            case R.id.iv_hotle:
                type = "住宿";
                ivHotle.setBackgroundResource(R.mipmap.account_hotel_pressed);
                break;
            case R.id.iv_ticket:
                type = "门票";
                ivTicket.setBackgroundResource(R.mipmap.account_tickets_pressed);
                break;
            case R.id.iv_shopping:
                type = "购物";
                ivShopping.setBackgroundResource(R.mipmap.account_shopping_pressed);
                break;
            case R.id.iv_entertainment:
                type = "娱乐";
                ivEntertainment.setBackgroundResource(R.mipmap.account_entertainment_pressed);
                break;


        }
    }
}
